package com.zhengyisky.service;

import com.zhengyisky.mapper.PurchaseMapper;
import com.zhengyisky.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    public List<Company> findAllCompany() {
        return purchaseMapper.findAllCompany();
    }

    public List<Project> findAllProject() {
        return purchaseMapper.findAllProject();
    }

    public List<Goods> findAllGoods() {
        return purchaseMapper.findAllGoods();
    }

    public List<Type> findAllType() {
        return purchaseMapper.findAllType();
    }

    @Transactional
    public void save(OrderEntity orderEntity) {
        Order order = orderEntity.getOrder();
        List<OrderDetail> orderDetailList = orderEntity.getOrderDetailList();

        if (order.getId() == null){
            //添加
            purchaseMapper.saveOrder(order);
            for (OrderDetail orderDetail : orderDetailList) {
                orderDetail.setOrderId(order.getId());
                purchaseMapper.saveOrderDetail(orderDetail);
            }
        }else{
            //修改
            purchaseMapper.updateOrder(order);

            for (OrderDetail orderDetail : orderDetailList) {
                if(orderDetail.getId() == null){
                    //新增订单行，添加
                    orderDetail.setOrderId(order.getId());
                    purchaseMapper.saveOrderDetail(orderDetail);
                }else{
                    //之前已有的数据，修改
                    purchaseMapper.updateOrderDetail(orderDetail);
                }
            }

            //删除不在orderDetailList中的数据
            purchaseMapper.deleteOrderDetail(orderEntity);

        }

    }

    public List<Order> list() {
        return purchaseMapper.list();
    }

    public List<Order> listByUser(Integer id) {
        return purchaseMapper.listByUser(id);
    }

    public List<Order> listByStatus() {
        return purchaseMapper.listByStatus();
    }

    public void submitApprove(Integer id) {
        purchaseMapper.submitApprove(id);
    }

    public Order findOrderById(Integer id) {
        return purchaseMapper.findOrderById(id);
    }

    public OrderEntity find(Integer id) {
        Order order = purchaseMapper.findOrderById(id);
        List<OrderDetail> list = purchaseMapper.findOrderDetailById(id);

        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrder(order);
        orderEntity.setOrderDetailList(list);

        return orderEntity;
    }

    public void approve(Integer id, Integer status) {
        purchaseMapper.approve(id, status);
    }

    public List<Order> listByTime(Date lastTime) {
        return purchaseMapper.listByTime(lastTime);
    }

    public Project findProjectById(Integer projectId) {
        return purchaseMapper.findProjectById(projectId);
    }

    public void updateProject(Integer projectId, Integer newTotalMoney) {
        purchaseMapper.updateProject(projectId, newTotalMoney);
    }
}
